package com.daka.controller;

import com.daka.enumerate.StatusCode;
import com.daka.service.AuctionTradeListService;
import com.daka.service.GoodsService;
import com.daka.vo.BaseResponseVo;
import com.daka.vo.DataResponseVo;
import com.daka.vo.response.ListTeamAuctionVo;
import com.daka.vo.response.ListUserAuctionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by yangx
 * CreateTime: 2016/12/29 17:17
 */

@Controller
@RequestMapping("/auctionRank")
@Slf4j
public class AuctionRankController {

    @Autowired
    private AuctionTradeListService auctionTradeListService;
    @Resource
    private GoodsService goodsService;


    @RequestMapping(method = RequestMethod.POST, value = "/teamList")
    @ResponseBody
    public DataResponseVo<Object> teamList(String a) {
        List<ListTeamAuctionVo> list = auctionTradeListService.selectTeamRank();
        HashMap<Long, ListTeamAuctionVo> hashMap = new HashMap<>();
        for (ListTeamAuctionVo team1 : list) {
            if (hashMap.containsKey(team1.getTeamId())) {
                ListTeamAuctionVo temp = hashMap.get(team1.getTeamId());
                temp.setAmount(temp.getAmount().add(team1.getAmount()));
                temp.setNumber(temp.getNumber() + 1);
                hashMap.put(team1.getTeamId(), temp);
            } else {
                hashMap.put(team1.getTeamId(), team1);
            }
        }
        List<ListTeamAuctionVo> list1 = new ArrayList<>();
        for (Long key : hashMap.keySet()) {
            list1.add(hashMap.get(key));
        }
        Collections.sort(list1);

        //总金额
        BigDecimal totalMoney = goodsService.sumTotal();
        if (totalMoney == null) {
            totalMoney = new BigDecimal("0.00");
        }
        //竞拍总次数
        int count = auctionTradeListService.countAll();
        Map<String, Object> map = new HashMap<>();
        map.put("retList", list1);
        map.put("totalMoney", totalMoney);
        map.put("count", count);
        return new DataResponseVo(StatusCode.OK.getCode(), StatusCode.OK.getMessage(), map);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/userList")
    @ResponseBody
    public DataResponseVo<Object> userList() {
        List<ListUserAuctionVo> list = auctionTradeListService.selectUserRank();
        HashMap<Long, ListUserAuctionVo> hashMap = new HashMap<>();

        for (ListUserAuctionVo auc : list) {
            if (hashMap.containsKey(auc.getUserId())) {
                ListUserAuctionVo temp = hashMap.get(auc.getUserId());
                temp.setAmount(temp.getAmount().add(auc.getAmount()));
                temp.setNumber(temp.getNumber() + 1);

                hashMap.put(auc.getUserId(), temp);
            } else {
                hashMap.put(auc.getUserId(), auc);
            }
        }
        List<ListUserAuctionVo> list1 = new ArrayList<>();
        for (Long key : hashMap.keySet()) {
            list1.add(hashMap.get(key));
        }
        Collections.sort(list1);
        int a = 1;
        for (ListUserAuctionVo vo : list1) {
            vo.setRanking(a);
            a = a + 1;
        }
        //总金额
        BigDecimal totalMoney = goodsService.sumTotal();
        if (totalMoney == null) {
            totalMoney = new BigDecimal("0.00");
        }
        //竞拍总次数
        int count = auctionTradeListService.countAll();
        Map<String, Object> map = new HashMap<>();
        map.put("retList", list1);
        map.put("totalMoney", totalMoney);
        map.put("count", count);

        return new DataResponseVo(StatusCode.OK.getCode(), StatusCode.OK.getMessage(), map);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/startPay")
    @ResponseBody
    public BaseResponseVo startPay() {
        auctionTradeListService.startOnlinePay();
        return new BaseResponseVo(StatusCode.OK.getCode(), StatusCode.OK.getMessage());
    }
}










